<?php
/**
 * Elgg Golf Score plugin
 *
 * @package ElggGolfScore
 */

elgg_register_event_handler('init', 'system', 'golfscore_init');

/**
 * Bookmark init
 */
function golfscore_init() {
	
	$root = dirname(__FILE__);
	elgg_register_library('elgg:golfscore', "$root/lib/golfscore.php");
	elgg_load_library('elgg:golfscore');
// 	elgg_register_menu_item('site', array(
// 			'name' => 'golfscore',
// 			'text' => elgg_echo('golfscore'),
// 			'href' => 'golfscore/all'
// 	));
	
	// actions
	$action_path = "$root/actions/golfscore";
	elgg_register_action('golfscore/save', "$action_path/save.php","public");
	elgg_register_action('golfscore/delete', "$action_path/delete.php");
	elgg_register_action('golfscore/save_course_card', "$action_path/save_course_card.php");
	elgg_register_action('golfscore/save_course', "$action_path/save_course.php");
// 	elgg_register_action('golfscore/share', "$action_path/share.php");

// 	elgg_register_plugin_hook_handler('register', 'menu:page', 'golfscore_page_menu');
	elgg_register_plugin_hook_handler('register', 'menu:owner_block', 'golfscore_owner_block_menu');
	//register sidebar
	elgg_register_plugin_hook_handler('view', 'sidebar_golfscore', 'golfscore_sidebar_handler');
	elgg_register_plugin_hook_handler('view', 'ownerblock_golfscore', 'golfscore_owner_score_handler');
	elgg_register_page_handler('golfscore', 'golfscore_page_handler');

	elgg_extend_view('css/elgg', 'golfscore/css');
	elgg_extend_view('js/elgg', 'golfscore/js');
	
	elgg_register_ajax_view('golfscore/fetch_course');
	elgg_register_ajax_view('golfscore/previous');
	
	elgg_register_ajax_view('golfscore/courses');
	elgg_register_ajax_view('golfscore/scores');
	elgg_register_ajax_view('golfscore/user_scores');
	elgg_register_ajax_view('golfscore/score_detail');
	elgg_register_ajax_view('golfscore/save_score');
	elgg_register_ajax_view('golfscore/edit_score');
	elgg_register_ajax_view('golfscore/map_courses');
	elgg_register_ajax_view('golfscore/map_hole');
	elgg_register_ajax_view('golfscore/map_hole_detail');
	elgg_register_ajax_view('golfscore/admin_map_hole_detail');
	elgg_register_ajax_view('golfscore/add');
	elgg_register_ajax_view('golfscore/edit');
	elgg_register_ajax_view('golfscore/login');
	elgg_register_ajax_view('golfscore/upload');
	elgg_register_ajax_view('forms/golfscore/save_course');
		//elgg_register_plugin_hook_handler('view', 'sidebar_golfscore', 'golfscore_sidebar_mobile_handler');
	
// 	elgg_register_widget_type('golfscore', elgg_echo('golfscore'), elgg_echo('golfscore:widget:description'));

// 	if (elgg_is_logged_in()) {
// 		$user_guid = elgg_get_logged_in_user_guid();
// 		$address = urlencode(current_page_url());

// 		elgg_register_menu_item('extras', array(
// 			'name' => 'golfscore',
// 			'text' => elgg_view_icon('push-pin-alt'),
// 			'href' => "golfscore/add/$user_guid?address=$address",
// 			'title' => elgg_echo('golfscore:this'),
// 			'rel' => 'nofollow',
// 		));
// 	}
// 	// Register granular notification for this type
// 	register_notification_object('object', 'golfscore', elgg_echo('golfscore:new'));

// 	// Listen to notification events and supply a more useful message
// 	elgg_register_plugin_hook_handler('notify:entity:message', 'object', 'golfscore_notify_message');

// 	// Register bookmarks view for ecml parsing
// 	elgg_register_plugin_hook_handler('get_views', 'ecml', 'golfscore_ecml_views_hook');

// 	// Register a URL handler for bookmarks
// 	elgg_register_entity_url_handler('object', 'golfscore', 'golfscore_url');

// 	// Register entity type for search
// 	elgg_register_entity_type('object', 'golfscore');

// 	// Groups
// 	add_group_tool_option('golfscore', elgg_echo('golfscore:enablegolfscore'), true);
// 	elgg_extend_view('groups/tool_latest', 'golfscore/group_module');
}

function golfscore_page_handler($page) {
	$______dir = dirname(__FILE__) . "/lib/golfscore.php";
	require_once($______dir);
	if (!isset($page[0])) {
		$page[0] = 'all';
	}
	elgg_push_breadcrumb(elgg_echo('golfscore'), 'golfscore/owner');
	
	// user usernames
	$user = get_user_by_username($page[0]);
	if ($user) {
		golfscore_url_forwarder($page);
	}
	$pages = dirname(__FILE__) . '/pages/golfscore';
	switch ($page[0]) {
		case "all":
			include "$pages/all.php";
			break;
			
		case "owner":
			include "$pages/owner.php";
			break;
			
		case "friends":
			include "$pages/friends.php";
			break;

		case "view":
			set_input('guid', $page[1]);
			include "$pages/view.php";
			break;

		case "add":
			gatekeeper();
			include "$pages/add.php";
			break;

		case "edit":
			gatekeeper();
			set_input('guid', $page[1]);
			include "$pages/edit.php";
			break;

		default:
			return false;
	}

	elgg_pop_context();
	return true;
}

function golfscore_url_forwarder($page) {
	global $CONFIG;

	if (!isset($page[1])) {
		$page[1] = 'items';
	}

	switch ($page[1]) {
		case "read":
			$url = "{$CONFIG->wwwroot}golfscore/view/{$page[2]}/{$page[3]}";
			break;
		case "inbox":
			$url = "{$CONFIG->wwwroot}golfscore/inbox/{$page[0]}";
			break;
		case "friends":
			$url = "{$CONFIG->wwwroot}golfscore/friends/{$page[0]}";
			break;
		case "add":
			$url = "{$CONFIG->wwwroot}golfscore/add/{$page[0]}";
			break;
		case "all":
			$url = "{$CONFIG->wwwroot}golfscore/all";
			break;
		case "items":
			$url = "{$CONFIG->wwwroot}golfscore/owner/{$page[0]}";
			break;
	}

	register_error(elgg_echo("changegolfscore"));
	forward($url);
}

function elgg_golfscore_page_handler($page) {
	global $CONFIG;

	elgg_set_page_owner_guid(elgg_get_logged_in_user_guid());
	
	// make a URL segment available in page handler script
	$page_type = elgg_extract(0, $page, 'all');
	$page_type = preg_replace('[\W]', '', $page_type);
	if ($page_type == 'owner') {
		$page_type = 'mine';
	}
	set_input('page_type', $page_type);

	//require_once("{$CONFIG->path}mod/golfscore/pages/golfscore/view.php");
	return true;
}

// /**
//  * Dispatcher for bookmarks.
//  *
//  * URLs take the form of
//  *  All bookmarks:        bookmarks/all
//  *  User's bookmarks:     bookmarks/owner/<username>
//  *  Friends' bookmarks:   bookmarks/friends/<username>
//  *  View bookmark:        bookmarks/view/<guid>/<title>
//  *  New bookmark:         bookmarks/add/<guid> (container: user, group, parent)
//  *  Edit bookmark:        bookmarks/edit/<guid>
//  *  Group bookmarks:      bookmarks/group/<guid>/all
//  *  Bookmarklet:          bookmarks/bookmarklet/<guid> (user)
//  *
//  * Title is ignored
//  *
//  * @param array $page
//  * @return bool
//  */
// function golfscore_page_handler($page) {

// 	elgg_load_library('elgg:bookmarks');

// 	if (!isset($page[0])) {
// 		$page[0] = 'all';
// 	}

// 	elgg_push_breadcrumb(elgg_echo('golfscore'), 'golfscore/all');

// 	// old group usernames
// 	if (substr_count($page[0], 'group:')) {
// 		preg_match('/group\:([0-9]+)/i', $page[0], $matches);
// 		$guid = $matches[1];
// 		if ($entity = get_entity($guid)) {
// 			golfscore_url_forwarder($page);
// 		}
// 	}

// 	// user usernames
// 	$user = get_user_by_username($page[0]);
// 	if ($user) {
// 		golfscore_url_forwarder($page);
// 	}

// 	$pages = dirname(__FILE__) . '/pages/golfscore';

// 	switch ($page[0]) {
// 		case "all":
// 			include "$pages/all.php";
// 			break;

// 		case "owner":
// 			include "$pages/owner.php";
// 			break;

// 		case "friends":
// 			include "$pages/friends.php";
// 			break;

// 		case "view":
// 			set_input('guid', $page[1]);
// 			include "$pages/view.php";
// 			break;
// 		case 'read': // Elgg 1.7 compatibility
// 			register_error(elgg_echo("changegolfscore"));
// 			forward("golfscore/view/{$page[1]}");
// 			break;

// 		case "add":
// 			gatekeeper();
// 			include "$pages/add.php";
// 			break;

// 		case "edit":
// 			gatekeeper();
// 			set_input('guid', $page[1]);
// 			include "$pages/edit.php";
// 			break;

// 		case 'group':
// 			group_gatekeeper();
// 			include "$pages/owner.php";
// 			break;

// 		case "bookmarklet":
// 			set_input('container_guid', $page[1]);
// 			include "$pages/bookmarklet.php";
// 			break;

// 		default:
// 			return false;
// 	}

// 	elgg_pop_context();
// 	return true;
// }

// /**
//  * Forward to the new style of URLs
//  *
//  * @param string $page
//  */
// function golfscore_url_forwarder($page) {
// 	global $CONFIG;

// 	if (!isset($page[1])) {
// 		$page[1] = 'items';
// 	}

// 	switch ($page[1]) {
// 		case "read":
// 			$url = "{$CONFIG->wwwroot}golfscore/view/{$page[2]}/{$page[3]}";
// 			break;
// 		case "inbox":
// 			$url = "{$CONFIG->wwwroot}golfscore/inbox/{$page[0]}";
// 			break;
// 		case "friends":
// 			$url = "{$CONFIG->wwwroot}golfscore/friends/{$page[0]}";
// 			break;
// 		case "add":
// 			$url = "{$CONFIG->wwwroot}golfscore/add/{$page[0]}";
// 			break;
// 		case "items":
// 			$url = "{$CONFIG->wwwroot}golfscore/owner/{$page[0]}";
// 			break;
// 		case "bookmarklet":
// 			$url = "{$CONFIG->wwwroot}golfscore/bookmarklet/{$page[0]}";
// 			break;
// 	}

// 	register_error(elgg_echo("changegolfscore"));
// 	forward($url);
// }

// /**
//  * Populates the ->getUrl() method for bookmarked objects
//  *
//  * @param ElggEntity $entity The bookmarked object
//  * @return string bookmarked item URL
//  */
// function golfscore_url($entity) {
// 	global $CONFIG;

// 	$title = $entity->title;
// 	$title = elgg_get_friendly_title($title);
// 	return $CONFIG->url . "golfscore/view/" . $entity->getGUID() . "/" . $title;
// }

// /**
//  * Add a menu item to an ownerblock
//  * 
//  * @param string $hook
//  * @param string $type
//  * @param array  $return
//  * @param array  $params
//  */
function golfscore_owner_block_menu($hook, $type, $return, $params) {
	$url = "golfscore/owner/{$params['entity']->username}";
	$item = new ElggMenuItem('golfscore', elgg_echo('golfscore'), $url);
	$return[] = $item;

	return $return;
}

function golfscore_sidebar_handler($hook, $type, $returnvalue, $params){
	return elgg_view('golfscore/list');
}
function golfscore_owner_score_handler($hook, $type, $returnvalue, $params){
	return elgg_view('golfscore/owner_score');
}

// /**
//  * Returns the body of a notification message
//  *
//  * @param string $hook
//  * @param string $entity_type
//  * @param string $returnvalue
//  * @param array  $params
//  */
// function golfscore_notify_message($hook, $entity_type, $returnvalue, $params) {
// 	$entity = $params['entity'];
// 	$to_entity = $params['to_entity'];
// 	$method = $params['method'];
// 	if (($entity instanceof ElggEntity) && ($entity->getSubtype() == 'golfscore')) {
// 		$descr = $entity->description;
// 		$title = $entity->title;
// 		$owner = $entity->getOwnerEntity();

// 		return elgg_echo('golfscore:notification', array(
// 			$owner->name,
// 			$title,
// 			$entity->address,
// 			$descr,
// 			$entity->getURL()
// 		));
// 	}
// 	return null;
// }

// /**
//  * Add a page menu menu.
//  *
//  * @param string $hook
//  * @param string $type
//  * @param array  $return
//  * @param array  $params
//  */
// function golfscore_page_menu($hook, $type, $return, $params) {
// 	if (elgg_is_logged_in()) {
// 		// only show bookmarklet in bookmark pages
// 		if (elgg_in_context('golfscore')) {
// 			$page_owner = elgg_get_page_owner_entity();
// 			if (!$page_owner) {
// 				$page_owner = elgg_get_logged_in_user_entity();
// 			}
			
// 			if ($page_owner instanceof ElggGroup) {
// 				$title = elgg_echo('golfscore:bookmarklet:group');
// 			} else {
// 				$title = elgg_echo('golfscore:bookmarklet');
// 			}

// 			$return[] = new ElggMenuItem('bookmarklet', $title, 'golfscore/bookmarklet/' . $page_owner->getGUID());
// 		}
// 	}

// 	return $return;
// }

// /**
//  * Return bookmarks views to parse for ecml
//  *
//  * @param string $hook
//  * @param string $type
//  * @param array  $return
//  * @param array  $params
//  */
// function golfscore_ecml_views_hook($hook, $type, $return, $params) {
// 	$return['object/golfscore'] = elgg_echo('item:object:golfscore');
// 	return $return;
// }
